package dfbz.com.dao;

import dfbz.com.dao.impl.BaseDaoImpl;
import dfbz.com.util.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.MapHandler;

import java.net.URLDecoder;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

public class ConJoinDao extends BaseDaoImpl {



    public void notice(List<Map<String, Object>> workers, Integer deptId, long total,Integer count) throws SQLException {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        String sql=null;
        Integer uid=null;
        for (Map<String, Object> worker:workers) {
            uid = (Integer) worker.get("id");
            sql = "insert into \n" +
                    "con_join\n" +
                    "values(?,?,?,?)";

            qr.update(sql,count,uid,total,0);
            count++;
        }


    }

    public Long getJoinMeetDetail(Integer id) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        Long total=null;
        String sql= "select * ,count(*) as total\n" +
                "from con_join\n" +
                "where c_id=? and status=0";

        Map<String, Object> map = null;
        try {
            map = qr.query(sql, new MapHandler(), id);
            total =(Long) map.get("total");
        } catch (SQLException e) {
            e.printStackTrace();
        }

           return total;
    }

    public void updateJoinStatus(Integer id,Integer mid) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        String sql="update con_join set status=1 where u_id=? and c_id=?";
        try {
            qr.update(sql,id,mid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public Integer findLastConferenceCount() {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        Integer id=null;
        String sql= " select max(id) as id\n" +
                "from conference";

        Map<String, Object> map = null;
        try {
            map = qr.query(sql, new MapHandler());
             id =(Integer) map.get("id");
            id++;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return id;

    }

    public Integer getConJoinCount() {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        Integer count=null;
        String sql= " select max(id) as count\n" +
                "from con_join";

        Map<String, Object> map = null;
        try {
            map = qr.query(sql, new MapHandler());
            count = (Integer) map.get("count");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return count;
    }
}
